package com.ls.lsqc.modules.sys.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lsqc.common.persistence.Page;
import com.ls.lsqc.common.service.CrudService;
import com.ls.lsqc.common.service.ServiceException;
import com.ls.lsqc.modules.sys.dao.ShopDao;
import com.ls.lsqc.modules.sys.dao.UserDao;
import com.ls.lsqc.modules.sys.entity.Dict;
import com.ls.lsqc.modules.sys.entity.Sequence;
import com.ls.lsqc.modules.sys.entity.Shop;
import com.ls.lsqc.modules.sys.utils.DictUtils;
import com.ls.lsqc.modules.sys.utils.SequenceUtil;
import com.ls.lsqc.modules.sys.utils.UserUtils;

@Service
@Transactional(readOnly = true)
public class ShopService extends CrudService<ShopDao, Shop>{
    
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private UserDao userDao;
    /**
     * 查询店铺列表
     * @return
     */
    public Page<Shop> findShop(Page<Shop> page, Shop shop) {
        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
//        shop.getSqlMap().put("dsf", dataScopeFilter(shop.getCurrentUser(), "o", "a"));
        // 设置分页参数
        shop.setPage(page);
        // 执行分页查询
        page.setList(shopDao.findList(shop));
        
        return page;
    }
    
    // 获取店铺信息
    public Shop getShop(Long id){
        
        Shop shop=new Shop();
        
        shop=shopDao.get(id);
        
        return shop;
    }
    
//    // 店铺名唯一性校验
//    public Shop getShoprByShopName(String ShopName){
//        return UserUtils.getByShopName(ShopName);
//    }
    
    /***
     * 通过店铺id找到关联人员ID
     * @param shop
     */
    
    public String getShopUser(String id){
        
        String userId=shopDao.getShopUser(id);
        
        return userId;
    }
    
    
    public List<Map<String,Object>> findShopStyle(String id){
        
        return shopDao.findShopStyle(id);
    }
    
    /**
     * 保存店铺信息和店铺管理员
     * @param shop
     */
    @Transactional(readOnly=false)
    public void saveShopOrUser(Shop shop){
        
        if (shop.getId()==null){
            
            // 取创建人创建时间
            shop.preInsert();
            // 插入店铺管理员 
            if(shop.getUser()!=null){
                
                shop.getUser().preInsert();
                userDao.insert(shop.getUser());
            }
            // 取出店铺主键ID
            shop.setUser(shop.getUser());
            
            Dict dict=DictUtils.getDictEntity(shop.getShopCity());
            // 店铺编号
            Sequence sequence=SequenceUtil.shopCode(dict.getItemShortNameEn());
            
            shop.setShopCode(sequence.getReturnCode());
            // 插入店铺信息
            shopDao.insert(shop);
            
            Map<Object, Object> paramMap =new HashMap<Object,Object>();  
            
            paramMap.put("shopId", shop.getId());
            paramMap.put("userId", shop.getUser().getId());
            // 插入店铺关联信息表
            shopDao.insertShopId(paramMap);
            
            shopDao.insertshopFundView(shop);
            
        }else{
            // 区更新时间
            shop.preUpdate();
            // 更新店铺人员信息
            if(shop.getUser()!=null){
                shop.getUser().preUpdate();
                userDao.update(shop.getUser());
            }
            shop.setVersion(shop.getVersion()+1);
            // 更新店铺信息
            shopDao.update(shop);
            
            shopDao.updateShopFundView(shop);
        }
     // 更新角色与款式关联
        shopDao.deleteShopStyle(shop);
        if (shop.getStyleLCodeList().size() > 0){
            shopDao.insertShopStyle(shop);
        }
        
        if (shop.getUser()!=null && shop.getUser().getId()!=null){
            // 更新用户与角色关联
            userDao.deleteUserRole(shop.getUser());
            if (shop.getUser().getRole() != null && shop.getUser().getRole().getId()!=null){
                userDao.insertUserRole(shop.getUser());
            }else{
                throw new ServiceException(shop.getUser().getLoginName() + "没有设置角色！");
            }
            // 清除用户缓存
        }
        UserUtils.clearCache();
        if(shop.getUser()!=null){
            UserUtils.clearCache(shop.getUser());
        }
    }
    
    /**
     * 删除店铺信息和店铺管理员
     * @param shop
     */
    @Transactional(readOnly=false)
    public void deletShopOrUser(Shop shop){
        
        // 区更新时间
        shop.preUpdate();
        // 更新店铺人员信息
        userDao.delete(shop.getUser());
        
        shop.setVersion(shop.getVersion()+1);
        // 更新店铺信息
        shopDao.delete(shop);
        
    }
    
    /**
     * 根据店铺名称获取店铺信息
     * @param shopName
     * @return
     */
    public Shop getShopByShopName(String shopName) {
        
        return shopDao.getShopByShopName(new Shop(null,shopName));
    }
    
}
